<template>
  <div>
    <div>
      <el-button type="success" size='small' @click="dialogVisible()" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
    </div>
    <el-table size='mini' ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55"></el-table-column>
      <el-table-column prop="name" :label="$t('策略名称')" align='center'>
        <template slot-scope="scope">
          <tooltip :html="scope.row.name" :content="scope.row.name" class="wid190" :refName="scope.row.name">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="local-ipv4" :label="$t('本地 IPv4 地址')" align='center'>
        <template slot-scope="scope">
          <tooltip :html="scope.row['local-ipv4']" :content="scope.row['local-ipv4']" class="wid190" :refName="scope.row['local-ipv4']">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="local-ipv6" :label="$t('本地 IPv6 地址')" align='center'>
        <template slot-scope="scope">
          <tooltip :html="scope.row['local-ipv6']" :content="scope.row['local-ipv6']" class="wid190" :refName="scope.row['local-ipv6']">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="right" align='center' fixed="right" :label="$t('操作')" width="120">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="dialogVisible(scope.row)" :class="'finds' + (scope.$index + 1)">{{$t('查看')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="record.page" :page-size="record.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>

    <nat46-dialog v-if="nat46DialogVisible" ref="refNat46DialogVisible"></nat46-dialog>
  </div>
</template>
<script>
import { getNat46Array, delInfo } from '@/api/network/address/nat46'
import Nat46Dialog from './components/Nat46Dialog'
export default {
  name: 'Nat46',
  components: {
    Nat46Dialog
  },
  data () {
    return {
      nat46DialogVisible: false,
      tableData: [],
      nameStr: '',
      record: {
        page: 1,
        limit: 20
      },
      total: 0
    }
  },
  created () {
    this.listData()
  },
  methods: {
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.record.limit = val
      this.record.page = 1
      this.listData()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.record.page = val
      this.listData()
    },
    // 多选按钮
    handleSelectionChange (data) {
      var nameStr = ''
      for (var item of data) {
        if (nameStr === '') {
          nameStr = item.name
        } else {
          nameStr = nameStr + '@#@' + item.name
        }
      }
      this.nameStr = nameStr
    },
    // 列表
    listData () {
      getNat46Array(this.record).then(res => {
        if (res.status === 0) {
          this.tableData = res.info
          this.total = res.count
        }
      })
    },
    // 添加按钮表单
    dialogVisible (data) {
      this.nat46DialogVisible = true
      this.$nextTick(() => {
        this.$refs.refNat46DialogVisible.init(data)
      })
    },
    deleteData () {
      if (this.nameStr) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delInfo({ name: this.nameStr }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.nameStr = ''
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    }
  }
}
</script>
